package com.xu.controller;

import com.xu.pojo.Product;
import com.xu.pojo.Stock;
import com.xu.service.ProductService;
import com.xu.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class StockController {
    @Autowired
    private StockService stockService;

    @Autowired
    private ProductService productService;

    @RequestMapping("/getStock")
    public String getStock(Model model)
    {
        List<Stock> stock = stockService.getStock();
        model.addAttribute("stock", stock);
        return "admin/stock/stock";
    }
    @RequestMapping("/toStockAdd")
    public String toStockAdd(Model model)
    {
        List<Product> list = productService.queryAllProduct();
        model.addAttribute("product", list);
        return "admin/stock/stock-add";
    }
    @RequestMapping("/stockAdd")
    public String addStock(Stock stock,Model model)
    {
        Stock stock1 = stockService.queryStockById(stock.getId());
        if (stock1!=null)
        {
            model.addAttribute("stock", stock1);
            model.addAttribute("msg", "产品已存在，请直接修改");
            return "admin/stock/stock";
        }
        else
        {
            stockService.addStock(stock);
            return "redirect:/getStock";
        }
    }
    @RequestMapping("/toUpdStock/{id}")
    public String toUpdStock(@PathVariable("id") int id, Model model)
    {
        Stock stock = stockService.queryStockById(id);
        model.addAttribute("stock", stock);
        return "admin/stock/stock-upd";
    }
    @RequestMapping("/updStock")
    public String updStock(Stock stock)
    {
        stockService.updStock(stock);
        return "redirect:/getStock";
    }
    @RequestMapping("/delStock/{id}")
    public String delStock(@PathVariable("id") int id)
    {
        stockService.delStock(id);
        return "redirect:/getStock";
    }
}
